﻿namespace PowerSong.SongDatabase.ObjectModel
    {
    using System.Collections.Generic;
    using System;

    public class Categories : ObjectModelBase
        {

        internal Categories(Database database) : base(database) { }

        public Category[] GetAllCategories()
            {

            var result = new List<Category>();
            foreach (var nextCategory in Database.Filter<Category>())
                {
                result.Add(nextCategory);
                }
            return result.ToArray();

            }

        public Category[] GetDefaultCategories()
            {

            var result = new List<Category>();
            foreach (var nextCategory in Database.Filter<Category>())
                {
                if (nextCategory.IsDefault == true) result.Add(nextCategory);
                }
            return result.ToArray();

            }

        public int Count()
            {
            return Database.Filter<Category>().Count;
            }

        public Category Get(Guid identifier)
            {
            return Database.Get<Category>(identifier);
            }

        public Category Get(string name, bool caseSensitive = false)
            {
            foreach (var category in Database.Filter<Category>())
                {
                if (caseSensitive)
                    {
                    if (category.Name == name) return category;
                    }
                else
                    {
                    if (category.Name.ToUpper() == name.ToUpper()) return category;
                    }
                }
            return null;
            }

        public Song[] GetSongs(Category category)
            {
            var result = new List<Song>();
            foreach (var song in Database.Songs.GetAllSongs())
                {
                if (song.Categories.Contains(category)) result.Add(song);
                }
            return result.ToArray();
            }

        /// <summary>
        /// Determines whether or not the given category exists in the database.
        /// </summary>
        /// <param name="songID">The ID of the category to search for in the database.</param>
        /// <returns>True, if the category exists in the database. False otherwise.</returns>
        public bool Contains(Guid categoryID)
            {
            return Get(categoryID) != null;
            }

        public bool Contains(string name, bool caseSensitive = false)
            {
            foreach (var category in Database.Filter<Category>())
                {
                if (caseSensitive)
                    {
                    if (category.Name == name) return true;
                    }
                else
                    {
                    if (category.Name.ToUpper() == name.ToUpper()) return true;
                    }
                }
            return false;
            }

        /// <summary>
        /// Creates a new category, or effectively edits an existing category.
        /// To create a category, supply an instance of the Category class that has been constructed.
        /// To edit an existing category, retrieve it from the database first, modify its fields, then supply that instance instead.
        /// </summary>
        /// <param name="category">The category to write to the database.</param>
        public void Write(Category category)
            {
            Database.Write(category);
            }

        public void Delete(Category category)
            {
            Database.Delete(category);
            }

        }

    }
